import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class VideoPlayer extends Canvas implements CommandListener, PlayerListener{
	private FileManagerMIDlet filemanager;
	private String url, filename;
	private Player player;
	private VideoControl vc;
	private VolumeControl vlc;
	private Command play, pause, back;
	public VideoPlayer(FileManagerMIDlet fm, String upurl, String filename){
		super();
		this.filemanager = fm;
		this.url = upurl;
		this.filename = filename;
		play = new Command("Phat", Command.OK, 0);
		pause = new Command("T.Dung", Command.OK, 0);
		back = new Command("Tro ve", Command.BACK, 1);
		addCommand(play);
		addCommand(back);
		setCommandListener(this);
		try {
      			player = Manager.createPlayer(url + filename);
			player.addPlayerListener(this);
      			player.realize();     
			player.prefetch();
			vlc = (VolumeControl) player.getControl("VolumeControl");
			if(vlc != null){
				vlc.setLevel(100);
			}		
      			if ((vc = (VideoControl)player.getControl("VideoControl")) != null) {
    				vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
   				vc.setVisible(true);
				vc.setDisplayFullScreen(true);
   			}
  			}
		catch (Exception pe) {} 
	}
	public void paint(Graphics g){}
	public void playerUpdate(Player player, String event, Object evenData){
		if(event == (PlayerListener.END_OF_MEDIA)){
			addCommand(play);
			removeCommand(pause);			
		}
	}
	public void Play(){
		try{
			player.start();
		}
		catch(Exception e){}
		removeCommand(play);
		addCommand(pause);		
	}
	public void Pause(){
		try{
			player.stop();
		}
		catch(Exception e){}
		removeCommand(pause);
		addCommand(play);
	}
	public void commandAction(Command c, Displayable d){
		if(c == play){
			Play();
		}
		if(c == pause){
			Pause();
		}
		if(c == back){
			player.close();
			filemanager.showContend(url,filename);
		}		
	}
}